#!/usr/bin/python
'''for more infomation about this problem, visit http://en.wikipedia.org/wiki/Pell%27s_equation'''
from math import sqrt
from p64 import getp

def gml(d):
    '''get the minimal solution in x for D=d, in equation x**2-D*y**2=1 '''
    if int(sqrt(d))**2 == d:
        #no solution
        return 0
    #get the representation of continued fraction of square root of n
    cf = getp(d)
    l = []        #the sublist of cf
    i = 0
    while True:
        #generate the continued fraction
        if i < len(cf[0]):
            l.append(cf[0][i])
        else:
            l.append(cf[1][(i-len(cf[0]))%len(cf[1])])
        f = getf(l)
        if f[1]**2 - d*(f[0]**2) == 1:
            return f[0]
        i += 1

def getf(l):
    '''get the fraction equal to the continued fraction represented by l
    return value is a tuple: (denominator, numerator)'''
    #iterate to get the result
    n = 1
    d = 0
    for k in l[::-1]:
        d,n = n,k*n + d
    return (d, n)
    
def main():
    m = 0
    mk = 0
    for k in range(1,1001):
        t = gml(k)
        if t>m:
            m=t
            mk=k
    print mk
#    print max([gml(k) for k in range(1,1001)])
if __name__ == '__main__':
    main()
    
